package com.atguigu.registration.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.registration.cmn.client.DictFeignClient;
import com.atguigu.registration.hosp.repository.HospitalRepository;
import com.atguigu.registration.hosp.service.HospitalService;
import com.atguigu.registration.model.hosp.Hospital;
import com.atguigu.registration.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Jiang Yizhi
 * @Date: 2022/4/24 8:33 AM
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;


    @Override
    public void save(Map<String, Object> paramMap) {
        /** 把参数map对象转换对象hospital */
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        /** 判断是否存在数据 */
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);
        /** 如果不存在就添加 */
        if (hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else { /** 如果存在就更新 */
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);

        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    /** 医院列表，条件分页查询 */
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        /** 创建Pageable对象 */
        Pageable pageable = PageRequest.of(page - 1, limit);
        /** 创建条件匹配器 */
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        /** 创建一个实例  */
        Example<Hospital> example =Example.of(hospital, matcher);
        /** 调用方法实现查询  */
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        /** 获取查询list集合，遍历进行医院等级封装 */
        pages.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });
        return pages;
    }

    /** 更新医院上线状态 */
    @Override
    public void updateStatus(String id, Integer status) {
        /** 根据id查询医院信息 */
        Hospital hospital = hospitalRepository.findById(id).get();
        /** 设置修改的值 */
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /** 获取医院详情信息 */
    @Override
    public Map<String, Object> getHospById(String id) {
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        Map<String, Object> result = new HashMap<>();
        result.put("hospital", hospital);
        /** 单独处理更直观*/
        result.put("bookingRule", hospital.getBookingRule());
        /** 不需要重复返回 */
        hospital.setBookingRule(null);
        return result;
    }

    /** 获取医院名称 */
    @Override
    public String getHosName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        /** 医院详情 */
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        /** 预约规则 */
        result.put("bookingRule", hospital.getBookingRule());
        /** 不需要重复返回 */
        hospital.setBookingRule(null);
        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital.getHosname();
    }


    /** 获取查询list集合，遍历进行医院等级封装 */
    private Hospital setHospitalHosType(Hospital hospital) {
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        /** 查询省市地区 */
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString);
        return hospital;
    }



}
